<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
class JmonoslideshowControllerOrder extends JController
{
	/**
	 * Custom Constructor
	 */
	function __construct( $default = array() )
	{
		$document = & JFactory::getDocument();
		$doc = &JDocument::getInstance('raw');
		$document = $doc;
		$document->setMimeEncoding('application/json') ;
		parent::__construct( $default );
	}
	
	/**
	* Search Tags
	*
	* @since 2.0
	*
	*/
	
	function saveOrder()
    {
		$listItem		= JRequest::getVar( 'listItem' );
		$listingCounter = 1;
		$db = JFactory::getDbo();
		foreach ($listItem as $listItemValue) {
	
			$query = "UPDATE ".$db->nameQuote('#__monoslideshow_media_category')." SET ordering = '" . $listingCounter . "' WHERE media_category_id = '" . (int)$listItemValue."'";
			echo $query;
			$db->setQuery($query);
			$db->query();
			
			if ($db->getErrorNum()) 
			{
				$this->setError($db->getErrorMsg());
				return false;
			}
			
			$listingCounter = $listingCounter + 1;
		}
		
		
		//$response = array("id" => $value, "label" => $value, "value" => $value);
		
		
		$this->_output($response);
	}
	
	
    
    
    /**
	* output response
	*
	* @since 2.0
	*
	* @param array $response 
	*
	* @return json response
	*/
	
	function _output($response)
    {
    	
		if( function_exists('json_encode') )
		{
			echo ( json_encode( $response ) );
		}
	else
		{
			//--seems we are in PHP < 5.2... or json_encode() is disabled
			require JPATH_COMPONENT_ADMINISTRATOR . DS. 'library' . DS .'json.php' ;
			$json = new Services_JSON();
			echo $json->encode( $response );
		}
		
    }
    
	
	
}
